// pagesDevice/pages/wmj/index.js
const app = getApp(),
  fetchApi = require("../../utils/httpPost.js"),
  common_Js = require("../../component/common.js")
import Toast from '../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    doorId: 0,
    pageIndex: 1,
    pageSize: 10,
    isBandCard: 0,
    isLoading: true,
    estateName: '',
    idCardNo: '',
    phoneNum: '',
    idCardId: 0,
    vipInfo: {},
    is_has_vip: 0,
    submitState: false,
    card_num: '',
    isShowPopup: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},
  onShow() {
    this.onHttpInit();
  },
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/self/vip/detail', {}, function (res) {
      console.log("获取二维码数据", res)
      if (res.data.code == 0) {
        console.log("订单列表数据", res)
        var vipInfo = res.data.data.vip;
        that.data.vipInfo = vipInfo;
        that.data.is_has_vip = res.data.data.is_has_vip;
        that.setData({
          hasData: true,
          is_has_vip: res.data.data.is_has_vip,
          vipInfo: vipInfo
        }) 
      }
      that.setData({
        isLoading: false,
        loading: true,
        netWork: true,
      })
      wx.stopPullDownRefresh() //停止下拉刷新
    }, function () {
      that.setData({
        netWork: false,
        hasData: false,
        isLoading: false,
      })
    })
  },
  inputHandle(e) {
    var value = e.detail.value;
    this.data.card_num = value;
  },
  handleAddCard() {
    this.setData({
      isShowPopup: !this.data.isShowPopup,
    })
  },
  scanCode: function (res) {
    console.log('scanCode res', res)
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    that.data.from_page = 'scancode'
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    } {
      wx.scanCode({
        success: (res) => {
          console.log("扫码结果", res);
          var qrcontent = res.result;
          console.log('扫码结果', qrcontent)
          var json_param = {}
          if (qrcontent.indexOf('https://') >= 0) {
            var q = "?" + qrcontent.split("?")[1];
            json_param = common_Js.getQueryVariable(q);
            if (qrcontent.indexOf('wxmini/selfip') >= 0) {
              that.onHttpBandCard(json_param.id, '');
            } else {
              Toast.fail("当前二维码无效");
            }
          }
        },
        fail: (res) => {
          console.log(res);
        }
      })
    }
  },
  onHttpBandCard: function (card_id, card_num) {
    var that = this;
    fetchApi.httpPost('/self/vip/band', {
      vip_id: card_id,
      card_num: card_num,
    }, function (res) {
      console.log("绑定会员卡", res)
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
        that.onHttpInit();
      } else {
        Toast.fail(res.data.msg);
      }
    }, function () {
      that.setData({
        netWork: false,
        hasData: false,
        isLoading: false,
      })
    })
  },
})